package com.yootk.main;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.*;

import java.util.Map;
import java.util.Properties;

/**
 * PropertyResolver属性解析
 */
public class Demo03_PropertyResolver {
    private static final Logger LOGGER = LoggerFactory.getLogger(Demo03_PropertyResolver.class);

    public static void main(String[] args) {
//        test1();
//        test2();
        test3();
    }

    /**
     * 实现属性的解析操作
     */
    public static void test1() {
        // 属性源1
        Map<String, Object> data = Map.of("yootk", "www.yootk.com", "jixianit", "www.jixianit.com");
        PropertySource mapSource = new MapPropertySource("url", data);
        // 属性源2
        Properties prop = new Properties();
        prop.put("java", "Java进阶开发实战");
        prop.put("spring", "Spring开发实战");
        PropertySource propSource = new PropertiesPropertySource("book", prop);
        // 追加属性源管理
        MutablePropertySources sources = new MutablePropertySources();
        sources.addLast(mapSource);
        sources.addLast(propSource);
        // 获取属性资源
        PropertySourcesPropertyResolver resolver = new PropertySourcesPropertyResolver(sources);
        LOGGER.info("属性资源获取：java = {}", resolver.getProperty("java"));
        LOGGER.info("属性资源获取：yootk = {}", resolver.getProperty("yootk"));
    }

    /**
     * 资源处理
     */
    public static void test2() {
        // 属性源1
        Map<String, Object> data = Map.of("yootk", "www.yootk.com", "jixianit", "www.jixianit.com");
        PropertySource mapSource = new MapPropertySource("url", data);
        // 属性源2
        Properties prop = new Properties();
        prop.put("java", "Java进阶开发实战");
        prop.put("spring", "Spring开发实战");
        PropertySource propSource = new PropertiesPropertySource("book", prop);
        // 追加属性源管理
        MutablePropertySources sources = new MutablePropertySources();
        sources.addLast(mapSource);
        sources.addLast(propSource);
        // 解析处理
        PropertySourcesPropertyResolver resolver = new PropertySourcesPropertyResolver(sources);
        // 如果能够查到，返回属性源中的数据
        LOGGER.info("资源表达式解析处理：java = {}", resolver.resolvePlaceholders("${java}"));
        // 如果不能够查到，返回自己设置的表达式或字符串
        LOGGER.info("资源表达式解析处理：lee = {}", resolver.resolvePlaceholders("${lee}"));
        LOGGER.info("资源表达式解析处理：yootk = {}", resolver.resolvePlaceholders("yootk"));
    }

    /**
     * 解析必须存在的数据
     */
    public static void test3() {
        // 属性源1
        Map<String, Object> data = Map.of("yootk", "www.yootk.com", "jixianit", "www.jixianit.com");
        PropertySource mapSource = new MapPropertySource("url", data);
        // 属性源2
        Properties prop = new Properties();
        prop.put("java", "Java进阶开发实战");
        prop.put("spring", "Spring开发实战");
        PropertySource propSource = new PropertiesPropertySource("book", prop);
        // 追加属性源管理
        MutablePropertySources sources = new MutablePropertySources();
        sources.addLast(mapSource);
        sources.addLast(propSource);
        // 解析处理
        PropertySourcesPropertyResolver resolver = new PropertySourcesPropertyResolver(sources);
        LOGGER.info("资源表达式解析处理：yootk = {}", resolver.resolvePlaceholders("${yootk}"));
        LOGGER.info("资源表达式解析处理：yootk = {}", resolver.resolveRequiredPlaceholders("${yootk}"));
    }
}
